﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ratatoskr.Toolbox
{
    public class SpriteDrawServer : SpriteDrawServer<Component>
    {
        public SpriteDrawServer(SpriteBatch spriteBatch, string type, bool open = true, bool single = false)
            : base(spriteBatch, type, open, single)
        {
        }

        public SpriteDrawServer(SpriteBatch spriteBatch, bool open = true, bool single = false)
            : base(spriteBatch, open, single)
        {
        }

        public SpriteDrawServer(string type, bool open = true, bool single = false)
            : base(type, open, single)
        {
        }

        public SpriteDrawServer(bool open = true, bool single = false)
            : base(open, single)
        {
        }
    }

    public class SpriteDrawServer<T> : UpdateServer<T> where T : class
    {
        protected SpriteBatch _sb;

        public SpriteDrawServer(SpriteBatch spriteBatch, string type, bool open = true, bool single = false)
            : base(type, open, single)
        {
            _sb = spriteBatch;
        }

        public SpriteDrawServer(SpriteBatch spriteBatch, bool open = true, bool single = false)
            : this(spriteBatch, "SpriteDrawServer", open, single)
        {
        }

        public SpriteDrawServer(string type, bool open = true, bool single = false)
            : this(null, type, open, single)
        {
        }

        public SpriteDrawServer(bool open = true, bool single = false)
            : this(null, "SpriteDrawServer", open, single)
        {
        }

        public void UseSpriteBatch(SpriteBatch spriteBatch)
        {
            _sb = spriteBatch;
        }

        protected override ServerComponentEventArgs ComposeArgs(object[] args = null)
        {
            Matrix m;
            Color c;
            SpriteTransform t;
            if (Parent.TryGetComponent<SpriteTransform>("SpriteTransform", out t))
            {
                m = t.Matrix;
                c = t.Color;
            }
            else
            {
                m = Matrix.Identity;
                c = Color.White;
            }
            return new ServerComponentEventArgs(new object[] { _gt, _sb, m, c, args });
        }

        protected override bool ExtendTriggerConditions()
        {
            return (_gt != null && _sb != null);
        }
    }
}